#include "usbd_core.h"
#include "usbd_audio.h"
#include "usbd_cdc.h"
#include "main.h"

#define USBD_VID           0x36b7
#define USBD_PID           0xffff
#define USBD_MAX_POWER     100
#define USBD_LANGID_STRING 1033
#define EP_INTERVAL        0x0a
#define CDC_INT_EP         0x81  
#define CDC_OUT_EP         0x02  
#define CDC_IN_EP          0x83  
#define AUDIO_IN_EP        0x85  

/* AUDIO Class Config */
#define AUDIO_FREQ         48000U
#define AUDIO_AS_INTERFACE 0x01

/* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */
#define AUDIO_IN_PACKET    ((uint32_t)((AUDIO_FREQ * 2 * 2) / 1000))
#define CDC_BUF_LENGTH     128U

#define USB_CONFIG_DESC_SIZ (unsigned long)      (9 +                                       \
                                                  AUDIO_AC_DESCRIPTOR_INIT_LEN(1) +         \
                                                  AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
                                                  AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
                                                  AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
                                                  AUDIO_AS_DESCRIPTOR_INIT_LEN(1) +         \
                                                  CDC_ACM_DESCRIPTOR_LEN)

#define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) +          \
                      AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
                      AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
                      AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)

static const uint8_t composite_descriptor[] = {    
    USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
    USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_DESC_SIZ, 0x04, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
    AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
    AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
    AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00),
    AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
    AUDIO_AS_DESCRIPTOR_INIT(AUDIO_AS_INTERFACE, 0x03, 0x02, AUDIO_IN_EP, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ)),
    CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, 0x02),
    ///////////////////////////////////////
    /// string0 descriptor
    ///////////////////////////////////////
    USB_LANGID_INIT(USBD_LANGID_STRING),
    ///////////////////////////////////////
    /// string1 descriptor
    ///////////////////////////////////////
    0x0A,                       /* bLength */
    USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
    'P', 0x00,                  /* wcChar0 */
    'U', 0x00,                  /* wcChar1 */
    'Y', 0x00,                  /* wcChar2 */
    'A', 0x00,                  /* wcChar3 */
    ///////////////////////////////////////
    /// string2 descriptor
    ///////////////////////////////////////
    0x28,                       /* bLength */
    USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
    'P', 0x00,                  /* wcChar0 */
    'U', 0x00,                  /* wcChar1 */
    'Y', 0x00,                  /* wcChar2 */
    'A', 0x00,                  /* wcChar3 */
    ' ', 0x00,                  /* wcChar4 */
    'C', 0x00,                  /* wcChar5 */
    'O', 0x00,                  /* wcChar6 */
    'M', 0x00,                  /* wcChar7 */
    'P', 0x00,                  /* wcChar8 */
    'O', 0x00,                  /* wcChar9 */
    'S', 0x00,                  /* wcChar10 */
    'I', 0x00,                  /* wcChar11 */
    'T', 0x00,                  /* wcChar12 */
    'E', 0x00,                  /* wcChar13 */
    ' ', 0x00,                  /* wcChar14 */
    'D', 0x00,                  /* wcChar15 */
    'E', 0x00,                  /* wcChar16 */
    'M', 0x00,                  /* wcChar17 */
    'O', 0x00,                  /* wcChar18 */
    ///////////////////////////////////////
    /// string3 descriptor
    ///////////////////////////////////////
    0x16,                       /* bLength */
    USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
    '2', 0x00,                  /* wcChar0 */
    '0', 0x00,                  /* wcChar1 */
    '2', 0x00,                  /* wcChar2 */
    '1', 0x00,                  /* wcChar3 */
    '0', 0x00,                  /* wcChar4 */
    '3', 0x00,                  /* wcChar5 */
    '1', 0x00,                  /* wcChar6 */
    '0', 0x00,                  /* wcChar7 */
    '0', 0x00,                  /* wcChar8 */
    '1', 0x00,                  /* wcChar9 */
#ifdef CONFIG_USB_HS
    ///////////////////////////////////////
    /// device qualifier descriptor
    ///////////////////////////////////////
    0x0a,
    USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
    0x00,
    0x02,
    0x00,
    0x00,
    0x00,
    0x40,
    0x01,
    0x00,
#endif
    0x00
};

volatile bool ep_tx_busy_flag = false;
volatile bool tx_flag         = 0;
volatile bool dtr_enable      = 0;
volatile bool cdc_rx_complete = 0;
static struct usbd_interface audio_intf0; 
static struct usbd_interface audio_intf1; 
static struct usbd_interface cdc_intf2;   
static struct usbd_interface cdc_intf3;


/* 1000hz sine wave */
USB_MEM_ALIGNX uint8_t mic_buf[AUDIO_IN_PACKET] = {
    0x00, 0x00, 0x00, 0x00, 0x5A, 0x08, 0x5A, 0x08, 0x90, 0x10, 0x90, 0x10, 0x7D, 0x18, 0x7D, 0x18,
    0xFF, 0x1F, 0xFF, 0x1F, 0xF5, 0x26, 0xF5, 0x26, 0x40, 0x2D, 0x40, 0x2D, 0xC5, 0x32, 0xC5, 0x32,
    0x6C, 0x37, 0x6C, 0x37, 0x20, 0x3B, 0x20, 0x3B, 0xD1, 0x3D, 0xD1, 0x3D, 0x73, 0x3F, 0x73, 0x3F,
    0xFF, 0x3F, 0xFF, 0x3F, 0x73, 0x3F, 0x73, 0x3F, 0xD1, 0x3D, 0xD1, 0x3D, 0x20, 0x3B, 0x20, 0x3B,
    0x6C, 0x37, 0x6C, 0x37, 0xC5, 0x32, 0xC5, 0x32, 0x40, 0x2D, 0x40, 0x2D, 0xF5, 0x26, 0xF5, 0x26,
    0xFF, 0x1F, 0xFF, 0x1F, 0x7D, 0x18, 0x7D, 0x18, 0x90, 0x10, 0x90, 0x10, 0x5A, 0x08, 0x5A, 0x08,
    0x00, 0x00, 0x00, 0x00, 0xA6, 0xF7, 0xA6, 0xF7, 0x70, 0xEF, 0x70, 0xEF, 0x83, 0xE7, 0x83, 0xE7,
    0x01, 0xE0, 0x01, 0xE0, 0x0B, 0xD9, 0x0B, 0xD9, 0xC0, 0xD2, 0xC0, 0xD2, 0x3B, 0xCD, 0x3B, 0xCD,
    0x94, 0xC8, 0x94, 0xC8, 0xE0, 0xC4, 0xE0, 0xC4, 0x2F, 0xC2, 0x2F, 0xC2, 0x8D, 0xC0, 0x8D, 0xC0,
    0x01, 0xC0, 0x01, 0xC0, 0x8D, 0xC0, 0x8D, 0xC0, 0x2F, 0xC2, 0x2F, 0xC2, 0xE0, 0xC4, 0xE0, 0xC4,
    0x94, 0xC8, 0x94, 0xC8, 0x3B, 0xCD, 0x3B, 0xCD, 0xC0, 0xD2, 0xC0, 0xD2, 0x0B, 0xD9, 0x0B, 0xD9,
    0x01, 0xE0, 0x01, 0xE0, 0x83, 0xE7, 0x83, 0xE7, 0x70, 0xEF, 0x70, 0xEF, 0xA6, 0xF7, 0xA6, 0xF7
};

USB_MEM_ALIGNX uint8_t cdc_buf[CDC_BUF_LENGTH] = {
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A
};

void usbd_configure_done_callback(void)
{
    
}

void usbd_audio_open(uint8_t intf)
{
    if (intf == AUDIO_AS_INTERFACE) {
        tx_flag = 1;
        /* setup first in ep write transfer */
        usbd_ep_start_write(AUDIO_IN_EP, mic_buf, AUDIO_IN_PACKET);
    }
}

void usbd_audio_close(uint8_t intf)
{
    if (intf == AUDIO_AS_INTERFACE) {
        tx_flag = 0;
    }
}

void usbd_audio_in_callback(uint8_t ep, uint32_t nbytes)
{
    USB_LOG_RAW("actual in len:%d\r\n", nbytes);
    if(tx_flag)
    {
      usbd_ep_start_write(AUDIO_IN_EP, mic_buf, AUDIO_IN_PACKET);
    }
}

static struct usbd_endpoint audio_in_ep = {
    .ep_cb = usbd_audio_in_callback,
    .ep_addr = AUDIO_IN_EP
};


void usbd_cdc_acm_bulk_out(uint8_t ep, uint32_t nbytes)
{
}

void usbd_cdc_acm_bulk_in(uint8_t ep, uint32_t nbytes)
{
    if ((nbytes % 64) == 0 && nbytes) {
        /* send zlp */
        usbd_ep_start_write(CDC_IN_EP, NULL, 0);
    } else {
        ep_tx_busy_flag = false;
    }
}

/*!< endpoint call back */
struct usbd_endpoint cdc_out_ep = {
    .ep_addr = CDC_OUT_EP,
    .ep_cb = usbd_cdc_acm_bulk_out
};

struct usbd_endpoint cdc_in_ep = {
    .ep_addr = CDC_IN_EP,
    .ep_cb = usbd_cdc_acm_bulk_in
};

void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
{
    if (dtr) {
        dtr_enable = 1;
    } else {
        dtr_enable = 0;
    }
}

void usb_init()
{
    usbd_desc_register(composite_descriptor);  
    usbd_add_interface(usbd_audio_init_intf(&audio_intf0));
    usbd_add_interface(usbd_audio_init_intf(&audio_intf1));
    usbd_add_endpoint(&audio_in_ep);
    usbd_audio_add_entity(0x02, AUDIO_CONTROL_FEATURE_UNIT);
  
    usbd_add_interface(usbd_cdc_acm_init_intf(&cdc_intf2));
    usbd_add_interface(usbd_cdc_acm_init_intf(&cdc_intf3));
    usbd_add_endpoint(&cdc_out_ep);
    usbd_add_endpoint(&cdc_in_ep);   

    usbd_initialize();
}

void usb_test()
{
  if (dtr_enable) {        
      ep_tx_busy_flag = true;
      usbd_ep_start_write(CDC_IN_EP, cdc_buf, CDC_BUF_LENGTH);
      while(ep_tx_busy_flag);   
    }
}
